﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

//STANDARD for WPF App
//STANDARD for WPF App

//STANDARD for WPF App
//STANDARD for WPF App
//For : InkCanvas
//For : ExecutedRoutedEventArgs

//For : DrawingAttributes
//For : XamlWriter
//For : DrawingGroup

//STANDARD for WPF App
//For : BitmapImage etc etc
//STANDARD for WPF App

//For : Geometric shapes like Line

namespace BitsOfStuff.Support
{
    /// <summary>
    /// Sets of suport functions for drawing on ink canvas
    /// </summary>
    internal class SupportDrawnFunctions
    {
        /// <summary>
        /// Function for dwawing single point on ink canvas
        /// </summary>
        /// <param name="inkCanvas">inkCanvas for drawing</param>
        /// <param name="myPoint">Point to drawn</param>
        /// <param name="color">Color of point</param>
        public static void drawnPoint(InkCanvas inkCanvas, MyPoint myPoint, Color color)
        {
            var pts = new StylusPointCollection
                          {
                              new StylusPoint(myPoint.getX(), myPoint.getY()),
                              new StylusPoint(myPoint.getX() + 8, myPoint.getY() + 8),
                              new StylusPoint(myPoint.getX() + 8, myPoint.getY() + 8),
                              new StylusPoint(myPoint.getX() + 8, myPoint.getY() + 8),
                              new StylusPoint(myPoint.getX() + 8, myPoint.getY() + 8)
                          };
            Stroke st = new CustomStroke(pts);
            st.DrawingAttributes.Color = color;
            inkCanvas.Strokes.Add(st);
        }

        public static void drawPoints(InkCanvas inkCanvas, MyPoint[] myPoints, bool[] bitMap, Color color)
        {
            for (int i = 0; i < myPoints.Length; i++)
            {
                var pts = new StylusPointCollection();
                if (bitMap[i])
                {
                    pts.Add(new StylusPoint(myPoints[i].getX(), myPoints[i].getY()));
                    pts.Add(new StylusPoint(myPoints[i].getX() + 8, myPoints[i].getY() + 8));
                    pts.Add(new StylusPoint(myPoints[i].getX() + 8, myPoints[i].getY() + 8));
                    pts.Add(new StylusPoint(myPoints[i].getX() + 8, myPoints[i].getY() + 8));
                    pts.Add(new StylusPoint(myPoints[i].getX() + 8, myPoints[i].getY() + 8));
                    Stroke st = new CustomStroke(pts);
                    st.DrawingAttributes.Color = color;
                    inkCanvas.Strokes.Add(st);
                }
            }
        }

        /// <summary>
        /// Function for drawing single line
        /// </summary>
        /// <param name="inkCanvas">Ink canvas for drawing</param>
        /// <param name="startPoint">Start point of line</param>
        /// <param name="endPoint">End point of line</param>
        private static void drawnLine(InkCanvas inkCanvas, MyPoint startPoint, MyPoint endPoint)
        {
            var myMyLine = new Line
                               {
                                   Stroke = Brushes.LightSteelBlue,
                                   X1 = startPoint.getX(),
                                   X2 = endPoint.getX(),
                                   Y1 = startPoint.getY(),
                                   Y2 = endPoint.getY(),
                                   HorizontalAlignment = HorizontalAlignment.Left,
                                   VerticalAlignment = VerticalAlignment.Center,
                                   StrokeThickness = 2
                               };
            inkCanvas.Children.Add(myMyLine);
        }

        /// <summary>
        /// Function for drawinf list of lines , lines are given as bitmaps give us info when point is selected to drawn
        /// </summary>
        /// <param name="inkCanvas">Ink canvas for drawing</param>
        /// <param name="myPoints">All points</param>
        /// <param name="bitMap">Choosen points</param>
        public void drawnLines(InkCanvas inkCanvas, MyPoint[] myPoints, bool[] bitMap)
        {
            for (int i = 0; i < bitMap.Length; i++)
            {
                if (bitMap[i])
                {
                    MyPoint startPoint = myPoints[i];
                    if (bitMap.Length > i + 1)
                        i++;
                    while (!bitMap[i])
                    {
                        if (bitMap.Length > i + 1)
                            i++;
                        else
                        {
                            return;
                        }
                    }
                    MyPoint endPoint = myPoints[i];
                    if (Math.Abs(startPoint.getX() + startPoint.getY() - (endPoint.getY() + endPoint.getX())) < 0.001)
                        return;

                    drawnLine(inkCanvas, startPoint, endPoint);
                    i--;
                }
            }
        }

        /// <summary>
        /// Clear inkCanvas from ours drawing
        /// </summary>
        /// <param name="inkCanvas">Ink Canvas rdy for cleaning</param>
        private static void clearInkCanvas(InkCanvas inkCanvas)
        {
            inkCanvas.Strokes.Clear();
            inkCanvas.Children.Clear();
        }

        /// <summary>
        /// Function that drawn suport line on inkCanvas
        /// </summary>
        /// <param name="inkCanvas">inkCanvas for lines</param>
        private static void drawnInkCanvasSupportLines(InkCanvas inkCanvas)
        {
            const double widthCanvas = 8 * 96;
            const double heightCanvas = 5 * 96;
            double y = 24;

            while (y < heightCanvas)
            {
                var line = new Line { X1 = 0, Y1 = y, X2 = widthCanvas, Y2 = y, Stroke = Brushes.LightBlue };
                inkCanvas.Children.Add(line);

                y += 24;
            }
        }

        /// <summary>
        /// Function that clean inkCanvas and redrawn lines
        /// </summary>
        /// <param name="inkCanvas"></param>
        public static void reDrawnInkCanvas(InkCanvas inkCanvas)
        {
            clearInkCanvas(inkCanvas);
            drawnInkCanvasSupportLines(inkCanvas);
        }

        private static bool _switchState;

        public static void switchTimerSig()
        {
            if (!_switchState)
            {
                MyParameters.timerSignal.Fill = new SolidColorBrush(Colors.Red);
            }
            else
            {
                var inkBck = new Color { R = 238, B = 137, G = 245 };
                MyParameters.timerSignal.Fill = new SolidColorBrush(inkBck);
            }
            _switchState = !_switchState;
        }

        /// <summary>
        /// Function that drawn specific rectangle on ink Canvas
        /// </summary>
        /// <param name="rectangle">Rectangle to drawn</param>
        public static void drawnRectangle(Rect rectangle)
        {
            var pts = new StylusPointCollection
                          {
                              new StylusPoint(rectangle.BottomLeft.X, rectangle.BottomLeft.Y),
                              new StylusPoint(rectangle.BottomRight.X, rectangle.BottomRight.Y),
                              new StylusPoint(rectangle.TopRight.X, rectangle.TopRight.Y),
                              new StylusPoint(rectangle.TopLeft.X, rectangle.TopLeft.Y),
                              new StylusPoint(rectangle.BottomLeft.X, rectangle.BottomLeft.Y)
                          };
            var inkRectangle = new Stroke(pts);

            MyParameters.inkCanvas.Strokes.Add(inkRectangle);
        }

        public static void drawnActionList(List<MyActions.MyAction> actions)
        {
            if (actions == null)
            {
                // MyParameters.debugText.Text = "Not awalyable footsteps";
                return;
            }
            var result = new StringBuilder();
            foreach (MyActions.MyAction myAction in actions)
            {
                if (myAction is MyActions.MyShowAction)
                {
                    var tmp = myAction as MyActions.MyShowAction;
                    char tmpChar = tmp.getChar();
                    if (myAction.isFinal())
                        result.Append("->");
                    result.AppendFormat("{0} ,", tmpChar);
                }
            }
            // MyParameters.debugText.Text = result.ToString();
        }
    }
}